package com.cgwx.auth.util.TreeUtil;



import com.cgwx.common.utils.GeneratorId;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 树形结构工具类，如：菜单、部门等
 *
 * @author zzj
 * @since 1.0.0
 */
public class TreeUtils {



    public static <T extends TreeNode> List<T> findNodeByUUID(List<T> treeNodes, String uuid) {
        //pid不能为空
        if(uuid == null)
            return null;
        List<T> treeList = new ArrayList<>();
        for(T treeNode : treeNodes) {
            if (uuid.equals(treeNode.getPid())) {
                treeList.add(treeNode);
            }
        }
        return treeList;
    }


    /**
     * 根据pid，构建树节点
     */
    public static <T extends TreeNode> List<T> build(List<T> treeNodes, String pid) {
        //pid不能为空
        if(pid == null)
            return null;

        List<T> treeList = new ArrayList<>();
        for(T treeNode : treeNodes) {
            if (pid.equals(treeNode.getPid())) {
                treeList.add(findChildren(treeNodes, treeNode));
            }
        }

        return treeList;
    }

    /**
     * 查找子节点
     */
    private static <T extends TreeNode> T findChildren(List<T> treeNodes, T rootNode) {
        for(T treeNode : treeNodes) {
            if(rootNode.getUuid().equals(treeNode.getPid())) {
                rootNode.getChildren().add(findChildren(treeNodes, treeNode));
            }
        }
        return rootNode;
    }

    /**
     * 构建树节点
     */
    public static <T extends TreeNode> List<T> build(List<T> treeNodes) {
        List<T> result = new ArrayList<>();

        //list转map
        Map<String, T> nodeMap = new LinkedHashMap<>(treeNodes.size());
        for(T treeNode : treeNodes){
            nodeMap.put(treeNode.getUuid(), treeNode);
        }

        for(T node : nodeMap.values()) {
            T parent = nodeMap.get(node.getPid());
            if(parent != null && !(node.getUuid().equals(parent.getUuid()))){
                parent.getChildren().add(node);
                continue;
            }

            result.add(node);
        }

        return result;
    }

    /**
     * 给树节点生成id
     * @param root
     * @param idPrefix
     * @param allGenerate true则所有节点全部生成新id；false如果当前节点有id则保留，无id则生成新id
     * @param <T>
     */
    public static <T extends TreeNode<T>> void generateTreeIdsByRoot(T root, String idPrefix, boolean allGenerate) {
        if (root == null) {
            return ;
        }
        String rootId = root.getUuid();
        if (allGenerate) {
            rootId = GeneratorId.generateId(idPrefix);
        } else {
            if (rootId == null || rootId.isEmpty()) {
                rootId = GeneratorId.generateId(idPrefix);
            }
        }
        root.setUuid(rootId);
        List<T> children = root.getChildren();
        for (int i = 0; i < children.size(); i++) {
            T tmp = children.get(i);
            tmp.setPid(rootId);
            generateTreeIdsByRoot(tmp, idPrefix, allGenerate);
        }
    }


    /**
     * 将树状结构转化为List
     */
    public static <T extends TreeNode<T>> List<T> root2List(T root) {
        List<T> list = new ArrayList<>();
        root2List(root, list);
        return list;
    }

    private static <T extends TreeNode<T>> void root2List(T root, List<T> list) {
        if (root == null) {
            return;
        }
        list.add(root);
        for (T child : root.getChildren()) {
            root2List(child, list);
        }
    }



//    public static <T extends TreeNode> List<T> build(List<T> treeNodes){}





}
